var miniGameId;
function template_action_holder(entry, merge) {

	var div = $("<div class='entry'>").css("border-left","8px solid "+get_team_color_from_team_id(entry["club"])).append(
		$("<div class='time'>").text(entry["time"]+"'")
	).hide();
	
	for (var i in entry["chance"]["text"]) {
		var text;
		if (merge) {
			text = entry["chance"]["text"][i].join("<br>");
			div.append(template_action_entry(text).hide())
		} else {
			for (var j in entry["chance"]["text"][i]) {
				text = entry["chance"]["text"][i][j];
				if (text) div.append(template_action_entry(text).hide())
			} 
		}
		//div.append(template_action_entry(entry["chance"]["text"][i]).hide())
	}

	return div;
}
function template_action_entry(text) {
	return get_text_from_chance(text);
}


function template_statistics() {

	if (end_of_game) {
		var possession_li = $("<li shots><span class='home'>"+match_data["match_data"]["possession"]["home"]+"%</span><span class='title bold'>Possession</span><span class='away'>"+match_data["match_data"]["possession"]["away"]+"%</span></li>");
	} else {
		var possession_li = $("<li possession><span style='width: auto;' class='bold'>Possession:</span><span style='width: auto; padding-left: 5px;'>Announced at Full-Time</span></li>");
	}

	var ul = $("<ul class='clean tcenter large_padding underlined stats three_parts'>").append(
		possession_li,
		$("<li shots><span class='home'>"+data_match_stats["shots"][0]+"</span><span class='title bold'>"+pagecontent[14]+"</span><span class='away'>"+data_match_stats["shots"][1]+"</span></li>"),
		$("<li shots_on_target><span class='home'>"+data_match_stats["shots_on_target"][0]+"</span><span class='title bold'>"+pagecontent[15]+"</span><span class='away'>"+data_match_stats["shots_on_target"][1]+"</span></li>"),
		$("<li set_pieces><span class='home'>"+data_match_stats["set_pieces"][0]+"</span><span class='title bold'>"+pagecontent[16]+"</span><span class='away'>"+data_match_stats["set_pieces"][1]+"</span></li>"),
		$("<li penalty><span class='home'>"+data_match_stats["penalty"][0]+"</span><span class='title bold'>"+pagecontent[17]+"</span><span class='away'>"+data_match_stats["penalty"][1]+"</span></li>"),
		$("<li yellow><span class='home'>"+data_match_stats["yellow"][0]+"</span><span class='title bold'>"+pagecontent[18]+"<img src='http://static.trophymanager.com/pics/icons/yellow_card.gif'/></span><span class='away'>"+data_match_stats["yellow"][1]+"</span></li>"),
		$("<li red><span class='home'>"+data_match_stats["red"][0]+"</span><span class='title bold'>"+pagecontent[19]+"<img src='http://static.trophymanager.com/pics/icons/red_card.gif'/></span><span class='away'>"+data_match_stats["red"][1]+"</span></li>")
	);
	
	ul["update"] = function(skip_animation) {
		for (stat in data_match_stats) {
			var elem = ul.find("["+stat+"] .home").first();
			if (elem.text() != data_match_stats[stat][0]) {
				elem.text(data_match_stats[stat][0]);
				if (!skip_animation) elem.effect("pulsate");
			}
			
			elem = ul.find("["+stat+"] .away").first();
			if (elem.text() != data_match_stats[stat][1]) {
				elem.text(data_match_stats[stat][1]);
				if (!skip_animation) elem.effect("pulsate");
			}
		}
	};
	
	return ul;
		
	//<a href='#' style='display:inline-block; padding-top:12px;'>Looking for the chat?</a>
}

function template_score() {
	var div = $("<div>").html("<span class='home'>0</span> - <span class='away'>0</span>");
	
	// Functions
	div["update"] = function(skip_animation) {
		var elem = div.find(".home").first();
		if (elem.text() != data_match_stats["score"][0]) {
			elem.text(data_match_stats["score"][0]);
			if (!skip_animation) elem.effect("pulsate");
		}
		
		elem = div.find(".away").first();
		if (elem.text() != data_match_stats["score"][1]) {
			elem.text(data_match_stats["score"][1]);
			if (!skip_animation) elem.effect("pulsate");
		}
	};
	
	return div;
}



function template_live_img() {
	return $("<img src='http://static.trophymanager.com/pics/matchviewer/live_badge.png'>");
}


function template_field_list(data) {
	
	
	//<li><a class='normal no_hover' href='/players/64520267'><div class='position'><span class='favposition short'><span class='d'>D</span></span></div><div class='name' >A. Bennedsen</div><div class='icons'></div></a></li>
	var ul = $("<ul class='player_list underlined_slim tleft'>");
	
	function template_li(player) {
		
		function get_img_src(stat) {
			var ret_ar = {
				"goal":"http://static.trophymanager.com/pics/icons/ball.gif",
				"yellow":"http://static.trophymanager.com/pics/icons/yellow_card.gif",
				"red":"http://static.trophymanager.com/pics/icons/red_card.gif",
				"yellow_red":"http://static.trophymanager.com/pics/icons/yellow_red_card.gif",
				"injury":"http://static.trophymanager.com/pics/icons/injury.gif",
				"out":"http://static.trophymanager.com/pics/icons/arrow_out.gif",
				"in":"http://static.trophymanager.com/pics/icons/arrow_in.gif",
				"mom":"http://static.trophymanager.com/pics/star.png"
			}
			return ret_ar[stat] || null;
		}
		
		var icons = $("<div class='icons'>");
		
		for (var i in player["match_stats"]) {
			icons.append(
				$("<img>").attr("src",get_img_src(player["match_stats"][i]))
			)
		}
	
		if (end_of_game) {
			var mom_icon = (player["mom"] == "1") ? $("<img src='http://static.trophymanager.com/pics/star.png'>") : "";
			var rating = $("<div>").addClass("rating");
			rating.text((player["rating"] == "0") ? "-" : player["rating"].toFixed(1) );
			rating.css("color", get_color_number_scope(player["rating"]));
			rating.append(mom_icon);
			icons.append(rating);
		}
		
		var li = $("<li>").append(
			$("<a class='normal no_hover' href='/players/"+player["player_id"]+"'>").append(
				$("<div class='position'>").append(
					color_favposition(player["start_position"], true)
				),
				$("<div class='name'>").text(player["name"]),
				icons
			)
		).tooltip(function() {
			return make_player_link(player["player_id"], miniGameId);
		})
		return li;
	}
	
	function populate(data) {
		// Convert to array
		var holder = [];
		for (var i in data) { holder.push(data[i]); }
		var data_sorted = holder.sort(sort_position).reverse();
	
		ul.empty();
		for (i in data_sorted) {
			ul.append(template_li(data_sorted[i]));
		}
		
	}	
	
	ul["update"] = function(data) {
		populate(data);
	}
	
	populate(data);
	
	return ul;
}



// QM
function template_qm() {

	if (!qm_mode) {
		return $("<div>").addClass("main qm").append(
			$("<span>").addClass("subtle italic").text("No QM Information Yet")
		);
	}

	var home_form = $("<span>");
	var away_form = $("<span>");
	/*
	home_team = data["club"]["home"];
	away_team = data["club"]["away"];
	*/
	for (var i in home_team["form"]) {
		home_form.append($("<a>").attr("href","/matches/"+home_team["form"][i]["id"]+"/").addClass("club_form").addClass(home_team["form"][i]["result"]).tooltip(wdl_language_ar[home_team["form"][i]["result"]]));
	}
	for (var i in away_team["form"]) {
		away_form.append($("<a>").attr("href","/matches/"+away_team["form"][i]["id"]+"/").addClass("club_form").addClass(away_team["form"][i]["result"]).tooltip(wdl_language_ar[away_team["form"][i]["result"]]));
	}

	function get_color(n) {
		n = parseInt(n);
		if (n > 0) {
			return "#46A92E";
		} else if (n < 0) {
			return "#842626";
		}
		return "#0033cc";
	}
	
	// QM UL
	var ul = $("<ul>").addClass("clean underlined large_padding three_parts").append(
		$("<li>").addClass("very_large").append(
			$("<span>").addClass("home").text(qm_mode["home"]["rank"]),
			$("<span>").addClass("title bold").text(pagecontent[66]),
			$("<span>").addClass("away").text(qm_mode["away"]["rank"])
		),
		$("<li>").append(
			$("<span>").addClass("home").text(qm_mode["home"]["rating"]["current"]),
			$("<span>").addClass("title bold").text(pagecontent[67]),
			$("<span>").addClass("away").text(qm_mode["away"]["rating"]["current"])
		),
		$("<li>").append(
			home_form,
			$("<span>").addClass("title bold").text(pagecontent[68]),
			away_form
		),
		$("<li>").append(
			$("<span>").addClass("home").css("color", get_color(qm_mode["home"]["rating"]["win"])).text(qm_mode["home"]["rating"]["win"]),
			$("<span>").addClass("title bold").text(pagecontent[69]),
			$("<span>").addClass("away").css("color", get_color(qm_mode["away"]["rating"]["loss"])).text(qm_mode["away"]["rating"]["loss"])
		),
		$("<li>").append(
			$("<span>").addClass("home").css("color", get_color(qm_mode["home"]["rating"]["draw"])).text(qm_mode["home"]["rating"]["draw"]),
			$("<span>").addClass("title bold").text(pagecontent[70]),
			$("<span>").addClass("away").css("color", get_color(qm_mode["away"]["rating"]["draw"])).text(qm_mode["away"]["rating"]["draw"])
		),
		$("<li>").append(
			$("<span>").addClass("home").css("color", get_color(qm_mode["home"]["rating"]["loss"])).text(qm_mode["home"]["rating"]["loss"]),
			$("<span>").addClass("title bold").text(pagecontent[71]),
			$("<span>").addClass("away").css("color", get_color(qm_mode["away"]["rating"]["win"])).text(qm_mode["away"]["rating"]["win"])
		)
	);
	
	return ul;

}



// Field
function template_field_div() {

	function template_field() {
	
		var field = $("<div>").addClass("player_field");

		function make_pog(player, home) {

			var x = (home) ? {"gk":9,"d":43,"dm":77,"m":111,"om":145,"f":179} : {"gk":407,"d":373,"dm":339,"m":305,"om":271,"f":237};
			var y = (home) ? {"l":47,"cl":102,"c":142,"cr":182,"r":237} : {"l":237,"cl":182,"c":142,"cr":102,"r":47};

			var line, side;
			// Use original line up if the game is over
			if (end_of_game) {
				line = get_player_line(player["start_position"]);
				side = get_player_side(player["start_position"]);

			// Else use the current line up
			} else {
				line = get_player_line(player["position"]);
				side = get_player_side(player["position"]);
			}

			if (side) {
				var div = $("<div>")
				.addClass("pog search "+line)
				.css({"margin-left":x[line], "margin-top":y[side]})
				.text((player["no"] || 0))
				.attr("player_id", player["player_id"])
				.tooltip(function() {
					return make_player_link(player["player_id"], miniGameId);
				})
				.click(function() {
					window.location = "/players/"+player["player_id"];
				}).append(
					$("<div>").addClass("mini_rec mini_rec_"+(player["rec"]*2))
				);

				return div;

			}
			return true;
		}
		
		// Make the field
		// Home
		for (var i in lineup["home"]) {
			var player = lineup["home"][i];
			field.append(make_pog(player, true))
		}

		// Away
		for (var i in lineup["away"]) {
			var player = lineup["away"][i];
			field.append(make_pog(player))
		}
		return field;
	}
	
	var field = template_field();
	
	// Expected line up?
	var expected = $("<div>");
	if (lineup["expected"]) expected.append($("<div>").css("color", "#AA0000").text(pagecontent[64]));
	
	// Add players out
	if (match_info["lineup_out"]) {
	
		// Injured/out players
		if (match_info["lineup_out"]["home"] != null && match_info["lineup_out"]["away"] != null) {
			
			function player_out_a(player_out) {
				if (!player_out) return "";
				return $("<a>").addClass("white normal").attr("href","/players/"+player_out["id"]+"/").html(player_out["name"]).tooltip(function() {
					return make_player_link(player_out["id"], miniGameId);
				});
			}
			
			players_out_ul = $("<ul>").addClass("std underlined clean questionable").append(
				$("<li class='bold'>").text(pagecontent[77])
			);
			
			var lineup_out = match_info["lineup_out"];
			
			expected.append(players_out_ul);
			
			function split_li() {
				return ;
			}
			
			var players_out_home_holder = new Array();
			var players_out_away_holder = new Array();
			
			for (var i in lineup_out["home"]) {
				players_out_home_holder.push(lineup_out["home"][i]);
			}
			for (var i in lineup_out["away"]) {
				players_out_away_holder.push(lineup_out["away"][i]);
			}
			
			for (var i=0; i<Math.max(players_out_home_holder.length,players_out_away_holder.length); i++) {
				players_out_ul.append(
					$("<li>").append(
						$("<span class='home text_left'>").append(player_out_a(players_out_home_holder[i])),
						$("<span class='away text_right'>").append(player_out_a(players_out_away_holder[i]))
					)
				);
			}
			
			
			/*
			for (var i in lineup_out["home"]) {
				players_out_ul.append(
					player_out_a(lineup_out["home"][i])
				)
			}
			
			for (var i in lineup_out["away"]) {
				players_out_ul.append(
					player_out_a(lineup_out["away"][i])
				)
			}
			*/
		}
	}
	
	var home_mentality, away_mentality, home_style, away_style;

	// Show original ment/style if the game is over
	if (end_of_game) {
		if (match_info["start_mentality"]) {
			home_mentality = home_style = parseInt(match_info["start_mentality"]["home"]) || 0;
			away_mentality = home_style = parseInt(match_info["start_mentality"]["away"]) || 0;
		}
		if (match_info["start_attacking_style"]) {
			home_style = parseInt(match_info["start_attacking_style"]["home"]) || 0;
			away_style = parseInt(match_info["start_attacking_style"]["away"]) || 0;
		}
	} else {
		if (match_info["mentality"]) {
			home_mentality = home_style = parseInt(match_info["mentality"]["home"]) || 0;
			away_mentality = home_style = parseInt(match_info["mentality"]["away"]) || 0;
		}
		if (match_info["attacking_style"]) {
			home_style = parseInt(match_info["attacking_style"]["home"]) || 0;
			away_style = parseInt(match_info["attacking_style"]["away"]) || 0;
		}
	}


	home_mentality = mentality_language_ar[home_mentality] || "-";
	away_mentality = mentality_language_ar[away_mentality] || "-";
	home_style = style_language_ar[home_style] || "-";
	away_style = style_language_ar[away_style] || "-";

	var details;
	try {
	
		details = $("<ul>").addClass("underlined clean versus_list").append(
			$("<li>").append(
				$("<span class='search text_right'>").text(home_mentality),
				$("<span>").addClass("bold").css("width", "165px").text(pagecontent[55]),
				$("<span class='search text_left'>").text(away_mentality)
			),
			$("<li>").append(
				$("<span class='search text_right'>").text(home_style),
				$("<span>").addClass("bold").css("width", "165px").text(pagecontent[54]),
				$("<span class='search text_left'>").text(away_style)
			)
		);

	} catch(e) {
		//err
	}

	
	var div = $("<div>").addClass("field_tab").append(
		template_nameplate,
		field,
		expected,
		details
	);
	
	function populate() {
		$(".player_field").replaceWith(template_field());
	}
	
	div["update"] = function(show_animation) {
		populate();
	}
	
	return div;
}


function template_nameplate() {
	
	var div = $("<div>").addClass("nameplate").append(
		$("<div class='home color' style='background-color:rgb("+team_colors["home"]["rgb"]+")'>"),
		$("<div class='away color' style='background-color:rgb("+team_colors["away"]["rgb"]+")'>"),
		$("<div class='overlay'>").append(
			$("<div class='home icon'>").append(
				$("<img src='"+home_team["logo_small"]+"' />")
			),
			$("<div class='away name bold' club_link='"+away_team["id"]+"'>"+away_team["club_name"]+" <img src='http://static.trophymanager.com/pics/flags/gradient/"+away_team["country"]+".png'></div>"),
			$("<div class='home name bold' club_link='"+home_team["id"]+"'>"+home_team["club_name"]+" <img src='http://static.trophymanager.com/pics/flags/gradient/"+home_team["country"]+".png'></div>"),
			$("<div class='away icon'>").append(
				$("<img src='"+away_team["logo_small"]+"' />")
			)
		)
	)
	
	return div;	
	
}

function template_enable_flash_button() {

	var div = $("<div class='abs flash_button_enable'>").append(
		$("<img src='http://static.trophymanager.com/pics/matchviewer/enable_button.png' />")
	)
	.click(enable_animations)
	.tooltip("Enable animations");
	
	return div;
	
}

function template_report_entry(entry, time) {

	time = time || 0;

	var outcome = $("<span>");
	var goal = false;
	var penalty = false;
	
	for (var j in entry["parameters"]) {
		if (entry["parameters"][j]) {
			var parameter = entry["parameters"][j];
			if (parameter["yellow"]) {
				outcome.append(
					$("<img src='http://static.trophymanager.com/pics/icons/yellow_card.gif'>"),
					template_player_link_a(parameter["yellow"])
				)
			}
			if (parameter["red"]) {
				outcome.append(
					$("<img src='http://static.trophymanager.com/pics/icons/red_card.gif'>"),
					template_player_link_a(parameter["red"])
				)
			}
			if (parameter["yellow_red"]) {
				outcome.append(
					$("<img src='http://static.trophymanager.com/pics/icons/yellow_red_card.gif'>"),
					template_player_link_a(parameter["yellow_red"])
				)
			}

			// Injury
			var injury;
			if (parameter["injury"]) {
				outcome.append(
					$("<img src='http://static.trophymanager.com/pics/icons/injury.gif'>"),
					template_player_link_a(parameter["injury"])
				)
			}

			if (parameter["sub"]) {
				outcome.append(
					$("<img src='http://static.trophymanager.com/pics/icons/arrow_out.gif'>"),
					template_player_link_a(parameter["sub"]["player_out"])
				)
				if (parameter["sub"]["player_in"]) {
					outcome.append(
						$("<img src='http://static.trophymanager.com/pics/icons/arrow_in.gif'>"),
						template_player_link_a(parameter["sub"]["player_in"])
					)
				}
			}

			if (parameter["penalty"]) {
				penalty = parameter["penalty"];
			}

			// Goal
			if (parameter["goal"]) {
				goal = true;
				if (penalty) {
					outcome.append(
						$("<img src='http://static.trophymanager.com/pics/icons/penalty.png'>"),
						template_player_link_a(parameter["goal"]["player"])
					)
				} else {
					var assist = "";
					try {
						if (parameter["goal"]["assist"]) {
							assist = $("<span>").append(" (",template_player_link_a(parameter["goal"]["assist"]),")")
						}
					} catch(e) {
					}
					outcome.append(
						$("<img src='http://static.trophymanager.com/pics/icons/ball.gif'>"),
						template_player_link_a(parameter["goal"]["player"]),
						assist
					)
				}
			}

			// Only show shot if it's NOT a goal
			if(parameter["shot"] && !goal) {

				// Penalty
				if (parameter["shot"]["target"] == "off" && penalty) {
					outcome.append(
						$("<img src='http://static.trophymanager.com/pics/icons/penalty_miss.png'>"),
						template_player_link_a(penalty)
					)

				} else if (parameter["shot"]["target"] == "off") {
					outcome.append($("<img src='http://static.trophymanager.com/pics/icons/missed_shot.png'>"))
				} else {
					outcome.append($("<img src='http://static.trophymanager.com/pics/icons/goalie_glove.png'>"))
				}
			}
		}
	}
	
	// Text
	var under_div = $("<div class='text small'>").css("display","none");
	for (var i in entry["chance"]["text"]) {
		for (var j in entry["chance"]["text"][i]) {
			text = entry["chance"]["text"][i][j];
			under_div.append(get_text_from_chance(text));
		}
	}
	
	// Header
	var li = $("<li>").attr("report_id", time).css("background-color",get_team_color_from_team_id(entry["club"]))
		.click(function() {
			$(this).find(".small").slideToggle(); 
			$(this).find(".header").toggleClass("opened"); 
		}).append(
		$("<div class='header'>").append(
			$("<span class='time'>").html(get_display_time(time*60).formatted_m),
			outcome,
			$("<span class='btn up'></span>")
		),
		under_div
	)
	return li;
}




function template_report() {

	var plus = $("<img class='all_button' src='http://static.trophymanager.com/pics/cf_mini_plus.png'>").click(function() {
		$("ul.report_list [report_id]").each(function() {
			$(this).find(".small").show();
			$(this).find(".header").addClass("opened"); 
		});
	});
	var minus = $("<img class='all_button' src='http://static.trophymanager.com/pics/cf_mini_minus.png'>").click(function() {
		$("ul.report_list [report_id]").each(function() {
			$(this).find(".small").hide(); 
			$(this).find(".header").removeClass("opened"); 
		});
	});
	
	var ul = $("<ul class='report_list clean underlined text_left'>");
	
	for (var i in report) {
		for (var j in report[i]) {
			if (report[i][j]) {
				ul.append(template_report_entry(report[i][j], i));
			}
		}
	}
	
	//var div = $("<div class='half'>").append(template_nameplate(), ul);
	
	var ret = $().add(plus).add(minus).add(ul);
	return ret;
}

// Event List
function template_events() {

	var ul = $("<ul class='event_list clean underlined large_padding text_left'>");
	
	for (var i in report) {
		for (var j in report[i]) {
			if (report[i][j]["parameters"]) {
				for (var m in report[i][j]["parameters"]) {
					ul.append(template_event_entry(report[i][j]["parameters"][m], i))
				}
			}
		}
	}
	
	return ul;
	
}


function template_event_entry(data, time) {
	
	var li = $("<li>").append(
		$("<span class='time'>"+time+"´</span><span class='home'></span><span class='score nowrap'></span><span class='away'></span>")
	)
	
	for (var j in data) {
		
		// Standard Events
		// Injury removed
		//var std_events_ar = ["yellow","red","yellow_red","injury"];
		var std_events_ar = ["yellow","red","yellow_red"];
		
		if ($.inArray(j,std_events_ar) >= 0) {
			var side = (get_team_side_from_player_id(data[j]) == 0) ? ".home" : ".away";
			li.find(side).append(
				$("<span>").append(template_player_link_a(data[j])).addClass(j)
			)
			return li;
		}
		
		// Goal
		if (j == "goal") {
			var side = (get_team_side_from_player_id(data[j]["player"]) == 0) ? ".home" : ".away";
			li.find(side).append(
				$("<span>").append(template_player_link_a(data[j]["player"])).addClass(j)
			);
			li.find(".score").text(data[j]["score"].join(" - "));
			return li;
		}
		
		// Sub
		/* SUB REMOVED
		if (j == "sub") {
			var side = (get_team_side_from_player_id(data[j]["player_in"]) == 0) ? ".home" : ".away";
			li.find(side).append(
				$("<span>").append(template_player_link_a(data[j]["player_in"])).addClass("out"),
				$("<span>").append(template_player_link_a(data[j]["player_out"])).addClass("in")
			);
			return li;
		}
		*/
	}
	return false;
}


// Venue/stadium
function template_venue_div() {
	
	function template_pitch_condition(n) {
		n = parseInt(n);
		if (n >= 100) { return $("<span>").text(pitch_condition_language_ar[7]).css("color", "#f90500"); }
		else if (n >= 97) { return $("<span>").text(pitch_condition_language_ar[6]).css("color", "#f90500"); }
		else if (n >= 90) { return $("<span>").text(pitch_condition_language_ar[5]).css("color", "#f86e00"); }
		else if (n >= 87) { return $("<span>").text(pitch_condition_language_ar[4]).css("color", "#e0e100"); }
		else if (n >= 79) { return $("<span>").text(pitch_condition_language_ar[3]).css("color", "#bae100"); }
		else if (n >= 74) { return $("<span>").text(pitch_condition_language_ar[2]).css("color", "#84de00"); }
		else if (n >= 71) { return $("<span>").text(pitch_condition_language_ar[1]).css("color", "#77dd00"); }
		else if (n >= 70) { return $("<span>").text(pitch_condition_language_ar[0]).css("color", "#2bbf00"); }
		return language["waiting_weather"]; //"Waiting for Weather Report";
		//
	}
	


	if (!venue) return $("<div>").addClass("main venue").append($("<span>").addClass("subtle italic").text(pagecontent[72]));


	// Venue UL
	var venue_top_ul = $("<ul class='clean underlined large_padding'>").append(
		$("<li class='mega_headline'>").html(venue["name"]),
		$("<li>").html(number_format(venue["capacity"])+" "+pagecontent[57])
	);

	var stadium_div = $("<div class='stadium_display'>").append(
		$("<img>").attr("src", get_weather_url(venue["weather"])).css({"position":"absolute","margin-left":"393px","margin-top":"7px"}),
		$("<img>").attr("src", "http://static.trophymanager.com/pics/matchviewer/venue_"+venue["picture"]+".png")
	).css({"text-align":"left"});

	
	function get_attendance() {
		console.log("att", (match_half_time !== false || end_of_game));
		return (match_half_time !== false || end_of_game) ? number_format(match_info["attendance"]) : language["tbah"];
	}
	

	var venue_bottom_ul = $("<ul class='clean underlined large_padding'>").append(
		$("<li>").append(
			$("<span class='bold'>").text(pagecontent[58]+" "),
			dateFormat(venue["kickoff"]*1000, "isoDate")
		),
		$("<li>").append(
			$("<span class='bold'>").text(pagecontent[62]+" "),
			get_match_type_lang(venue["matchtype"])
		),
		$("<li>").append(
			$("<span class='bold'>").text(pagecontent[59]+" "),
			(venue["tournament"] || "-")
		),
		$("<li>").append(
			$("<span class='bold'>").text(pagecontent[60]+" "),
			$("<span class='attendance'>").text(get_attendance())
		),
		$("<li>").append(
			$("<span class='bold'>").text(pagecontent[61]+" "),
			template_pitch_condition(venue["pitch_condition"])
		)
	);
	
	var div = $("<div>");
	
	div["update"] = function() {
		venue_bottom_ul.find(".attendance").text(get_attendance());
	}
	
	return div.append(venue_top_ul, stadium_div, venue_bottom_ul);
	
}

function template_player_link_a(id, name) {
	if (!name) {
		name = get_player_from_id(id)["name"];
	}
	if (!name) return $("<span>").text("Retired");
	return $("<a href='/players/"+id+"'>"+name+"</a>").addClass("none").tooltip(function() {
		return make_player_link(id, miniGameId);
	});
}

function template_top_nt_names(name, nickname, id) {
	return $("<a href='/national-teams/"+id+"/' class='very_large white'>"+name+"</a><div>("+nickname+")</div>");
}

function template_top_club_names(name, nickname, id) {
	return $("<a club_link='"+id+"/' href='/club/"+id+"/' class='very_large white'>"+name+"</a><div>("+nickname+")</div>");
}

function template_control_button(icon_classes, classes) {
	return $("<div>").addClass("control_button").addClass(classes).append(
		$("<div>").addClass(icon_classes)
	);
}

/*
function template_color_line_span(position) {
	if (position) {
		var color_ar = {"gk":"286A00","d":"0076C5","dm":"FFCC00","m":"FFCC00","om":"FFCC00","f":"CC0000"};
		var line = get_player_line(position);
		if (color_ar[line]) {
			return $("<span>").css("color", "#"+color_ar[line]).text(line.toUpperCase());
		}
		return $("<span>").css("color", "#555").text(pagecontent[63]);
	}
	return $("<span>");
}
*/



/*
function template_team_name_header() {

	var home_name = (home_team["club_name"].length>18) ? home_team["club_name"].substr(0,18) : home_team["club_name"];
	var away_name = (away_team["club_name"].length>18) ? away_team["club_name"].substr(0,18) : away_team["club_name"];
	
	if (nt_mode) {
		var home_logo = $("<div>").addClass("home icon").html("<img src='http://static.trophymanager.com/pics/nt_logos/25px/"+home_team["country"]+".png' />");
		var away_logo = $("<div>").addClass("away icon").html("<img src='http://static.trophymanager.com/pics/nt_logos/25px/"+away_team["country"]+".png' />");
		
		var home_name = $("<div>").addClass("home name bold").html(home_team["club_name"]);
		var away_name = $("<div>").addClass("away name bold").html(away_team["club_name"]);
		
	} else {
		var home_logo = $("<div>").addClass("home icon").html("<img src='"+home_team["logo_small"]+"' />");
		var away_logo = $("<div>").addClass("away icon").html("<img src='"+away_team["logo_small"]+"' />");
		
		var home_name = $("<div>").addClass("home name bold").attr("club_link", home_team["id"]).html(home_team["club_name"]+" <img src='http://static.trophymanager.com/pics/flags/gradient/"+home_team["country"]+".png'>");
		var away_name = $("<div>").addClass("away name bold").attr("club_link", away_team["id"]).html(away_team["club_name"]+" <img src='http://static.trophymanager.com/pics/flags/gradient/"+away_team["country"]+".png'>");
	}

	return $("<div>").addClass("nameplate").append(
		$("<div>").addClass("home color").css("background-color","rgb("+team_colors["home"]["rgb"]+")"),
		$("<div>").addClass("away color").css("background-color","rgb("+team_colors["away"]["rgb"]+")"),
		$("<div>").addClass("overlay").append(
			home_logo,
			home_name,
			away_name,
			away_logo
		)
	)

}
*/

function template_live_scores_scorer_entry(data) {
	var div = $("<div class='scorer small subtle'>");
	
	for (var i in data) {
		div.append(
			$("<div>").append(
				$("<span>").text(data[i]["time"]),
				$("<a class='subtle' href='/players/"+data[i]["id"]+"'>").html(data[i]["name"]),
				$("<span>").text(data[i]["score"].join(" - "))
			)
		)
	}
	
	return div;
}

function template_head_to_head_list(data) {
	// List
	var ul = $("<ul class='live_scores clean large_padding underlined'>");
	
	
	var seasons = [];
	for (var season in data) {
		seasons.push(season);
	}
	seasons.reverse();

	for (var i in seasons) {
		season = seasons[i];
		ul.append($("<li>").text("Season "+season));
		
		for (var i in data[season]) {
		
			var match = data[season][i];
			
			var home_team_name = (get_team_side_from_team_id(match["hometeam"])) ? away_team["club_name"] : home_team["club_name"];
			var away_team_name = (get_team_side_from_team_id(match["awayteam"])) ? away_team["club_name"] : home_team["club_name"];
			
			var score_html = (parseInt(SESSION["season"]) == parseInt(season)) ? $("<a href='/matches/"+match["id"]+"' class='score white normal'>").text(match["result"]).attr("match_link", match["id"]) : $("<span class='score white normal'>").text(match["result"]).attr("match_link", match["id"]).attr("id", "match"+match["id"]).attr("match_season", season);
			
			var li = $("<li>").append(
				$("<span class='subtle'>").text(get_match_type_lang(match["matchtype"])),
				$("<a href='/club/"+match["hometeam"]+"' class='home white normal'>").text(home_team_name),
				score_html,
				$("<a href='/club/"+match["awayteam"]+"' class='away white normal'>").text(away_team_name)
			);
			
			ul.append(li);
		}
	}
	
	activate_match_links(ul.find("[id^='match']"));
	
	var div = $("<div class='half'>").append(ul);
	
	return div;

}




function template_head_to_head_overview(data) {
	
	if (data.length < 1) {
		return pagecontent[92];
	}

	var match_type_stats_ar = {
		"all":{},
		"international":{},
		"cup":{},
		"league":{},
		"friendly":{}
	};

	function add_to_ar(type, team_id) {
		console.log(type);
		if (!match_type_stats_ar[type][team_id]) {
			match_type_stats_ar[type][team_id] = 0;
		}
		match_type_stats_ar[type][team_id]++;
	}
	
	for (var season in data) {
		for (var i in data[season]) {
		
			var match = data[season][i];
			
			var match_type = get_match_type(match["matchtype"]);
			var result_ar = match["result"].split("-");
			
			// Home win
			if (result_ar[0] > result_ar[1]) {
				add_to_ar(match_type, match["hometeam"]);
				add_to_ar("all", match["hometeam"]);
				
			// Away win
			} else if (result_ar[0] < result_ar[1]) {
				add_to_ar(match_type, match["awayteam"]);
				add_to_ar("all", match["awayteam"]);
			
			// Draw
			} else {
				add_to_ar(match_type, 0);
				add_to_ar("all", 0);
			}
			
		}
	}
	
	// List
	var div = $("<div class='half'>").append(template_nameplate);
	
	var type_div = $("<ul class='live_scores clean large_padding underlined'>");
	
	for (var type in match_type_stats_ar) {
		
		if (match_type_stats_ar[type][home_team["id"]] > 0 || match_type_stats_ar[type][0] > 0 || match_type_stats_ar[type][away_team["id"]] > 0) {
			
			var text;
			var total_classes = "";
			if (type == "all") {
				text = "Total";
				total_classes = "bold very_large";
			} else if (type == "international") {
				text = match_type_language_ar["international_cup"];
			} else if (type == "cup") {
				text = match_type_language_ar["cup"];
			} else if (type == "league") {
				text = match_type_language_ar["league"];
			} else if (type == "friendly") {
				text = match_type_language_ar["friendly"];
			}

			
			type_div.append(
				$("<li class='head_to_head_overview'>").addClass(total_classes).append(
					$("<div class='lable'>").text(text),
					$("<div>").append(match_type_stats_ar[type][home_team["id"]]||0),
					$("<div>").append(match_type_stats_ar[type][0]||0),
					$("<div>").append(match_type_stats_ar[type][away_team["id"]]||0)
				)
			);
			
			div.append(type_div);
		
		}
		
	}
	
	
	return div;

}

function template_live_scores(data) {

	// Top div
	var top = $("<div class='dark tcenter large padding round_corners'>"+pagecontent[94]+"</div>");
	
	// List
	var ul = $("<ul class='live_scores clean large_padding underlined'>");
	for (var i in data) {
		var match = data[i];
		
		var li = $("<li>").attr("match_id", match["match_id"]).append(
			$("<a href='/club/"+match["home"]["id"]+"' class='home white normal'>").text(match["home"]["name"]),
			$("<a href='/matches/"+match["match_id"]+"' class='score white normal'>").text("0 - 0"),
			$("<a href='/club/"+match["away"]["id"]+"' class='away white normal'>").text(match["away"]["name"]),
			$("<div class='scorer'>")
		);
		ul.append(li);
	}
	
	
	
	var div = $("<div class='half'>").append(top,ul);
	
	function update() {
		
		for (var i in live["matches"]) {
			var match = live["matches"][i];
			
			var li = ul.find("[match_id="+match["match_id"]+"]");
			li.find(".score").text(match["score"].join(" - "));
			li.find(".scorer").replaceWith(template_live_scores_scorer_entry(match["scorers"]));
			
		}
	}
	
	div["update"] = function() { update(); };
	
	return div;
	
}


function template_live_standings() {

	// Top div
	var top = $("<div class='dark tcenter large padding round_corners'>"+pagecontent[95]+"</div>");
	
	// List
	var ul = $("<ul class='underlined clean team_standings large_padding'>");

	function make_lis() {
	
		ul.empty();
	
		var standings_ar = new Array();
		for (var i in live["league"]) {
			standings_ar.push(live["league"][i]);
		}

		standings_ar.sort(sort_standing);

		for (var i in standings_ar) {
			var team = standings_ar[i];
			var number = parseInt(i)+1;
			var standings_indicator = (team["old_standing"] == number) ? "" : (team["old_standing"] < number) ? "down" : "up";
			var li = $("<li>").attr("club_id", team["id"]).append(
				$("<span>").text(number),
				$("<span>").append(
					$("<div class='standings_indicator "+standings_indicator+"'>")
				),
				$("<a href='/club/"+team["id"]+"/' class='team white normal'>").text(team["name"]),
				$("<span class='subtle goal_dif'>").text(team["goals_difference"]+team["bonus_goals"]),
				$("<span class='score'>").text(team["points"]+team["bonus"])
			);
			ul.append(li);
		}
	
	}
	
	var div = $("<div class='half'>").append(top,ul);
	make_lis();
	
	div["update"] = function() {
		make_lis();
	}
	
	return div;
	
}


// ^^DONE^^




/*
// Live scores
function template_live["scores"]_div(skip_animation) {

	update_league_matches_data();

	// Scores UL
	var scores_ul = $("<ul>").addClass("live["scores"] big");

	var delay = 0;

	function get_scores_li(action) {

		delay += delay_bump;

		var score = [0,0];
		try {
			if (action["score"]) score = action["score"];
		} catch(e) {
		}

		var home = $("<a>").attr("href", "/club/"+action["home"]["id"]).addClass("home white normal").text(action["home"]["name"]);
		var away = $("<a>").attr("href", "/club/"+action["away"]["id"]).addClass("away white normal").text(action["away"]["name"]);
		var score = $("<a>").attr("href", "/matches/"+action["match_id"]).addClass("score white normal").text(score.join(" - "));

		var scorers = $("<div>");
		for (var i in action["scorers"]) {
			scorers.append(
				$("<div>").addClass("small scorer").append(
					$("<span>").html(action["scorers"][i]["time"]),
					template_player_link_a(action["scorers"][i]["player_id"], action["scorers"][i]["name"]),
					$("<span>").text(action["scorers"][i]["score"][0]+"-"+action["scorers"][i]["score"][1])
				)
			);
		}

		var li = $("<li>").attr("match_id", action["match_id"]).append(home, score, away, scorers);
		if (!skip_animation) {
			// Fade in
			li.css("opacity",0).delay(delay).animate({"opacity":1}, 200);
		}
		return li;
	}

	for (var i in league_data) {
		scores_ul.append(get_scores_li(league_data[i]));
	}

	var standings = template_live["scores"]_standings_uls();
	return $("<div>").addClass("main live["scores"]").append(scores_ul);

}
*/
/*
// Live scores and standings
function template_live["scores"]_standings_uls(skip_animation) {

	// Standings UL
	var standings_ul_1 = $("<ul>").addClass("league_standings_list");
	var standings_ul_2 = $("<ul>").addClass("league_standings_list");

	delay = 0;

	function get_standings_li(action, standing) {

		delay += delay_bump;

		action["old_standing"] = parseInt(action["old_standing"]);

		var indicator = (action["old_standing"] == standing) ? "" : (action["old_standing"] >= standing) ? "up" : "down";

		var li = $("<li>").append(
			$("<span>").text(standing),
			$("<span>").append(
				$("<div>").addClass("league_live_standings_indicator").addClass(indicator)
			),
			$("<a>").attr("href", "/club/"+action["id"]).addClass("team white normal").text(action["name"])
		);

		// Fade in
		if (!skip_animation) li.css("opacity",0).delay(delay).animate({"opacity":1}, 200);

		return li;
	}

	var standings_ar = new Array();
	for (var i in league_teams) {
		standings_ar.push(league_teams[i]);
	}

	standings_ar.sort(sort_standing);

	var standing = 0;
	for (var i in standings_ar) {
		standing++;
		if (standing < 10) {
			standings_ul_1.append(get_standings_li(standings_ar[i], standing));
		} else {
			standings_ul_2.append(get_standings_li(standings_ar[i], standing));
		}
	}

	return [standings_ul_1,standings_ul_2];

}

function template_live["scores"]_compact_ul(skip_animation) {

	delay = 0;

	function get_scores_li(action) {

		delay += delay_bump;

		var score = action["score"] || [0,0];
		var home = $("<a>").attr("href", "/club/"+action["home"]["id"]).addClass("home white normal").text(action["home"]["name"]);
		var away = $("<a>").attr("href", "/club/"+action["away"]["id"]).addClass("away white normal").text(action["away"]["name"]);
		var score = $("<a>").attr("href", "/matches/"+action["match_id"]).addClass("score white normal").text(score.join(" - "));

		var li = $("<li>").attr("match_id", action["match_id"]).addClass(action["id"]).append(home, score, away);
		// Fade in
		if (!skip_animation) { li.css("opacity",0).delay(delay).animate({"opacity":1}, 200); }

		return li;
	}

	// Scores UL
	var scores_ul = $("<ul>").addClass("live["scores"] compact");

	for (var i in league_data) {
		scores_ul.append(get_scores_li(league_data[i]));
	}

	return scores_ul;

}
*/
/*
function template_live_compact_div(skip_animation) {
	var standings = template_live["scores"]_standings_uls(skip_animation);
	return $("<div>").addClass("main").append(
		template_live["scores"]_compact_ul(skip_animation),
		$("<span>").addClass("standings_compact_left").append(standings[0]),
		$("<span>").addClass("standings_compact_right").append(standings[1])
	);
}
*/
/*
function template_buy_pro_div(height) {
	height = Math.max(height, 200);
	var under = $("<div>").addClass("under").css("height", height+"px");
	var over = $("<div>").addClass("over").html("You need <img class='vmiddle' src='http://static.trophymanager.com/pics/pro_icon.png'> to get the second half updates.<br><a class='arrow_right' href='/buy-pro/'>Buy PRO</a><br><a class='arrow_right' href='/free-pro/'>Get Free PRO</a>").css("margin-top", Math.round((height-60)/2)+"px");
	return $("<div>").addClass("buy_pro").append(under.append(over));;
}

*/
